// pages/search/search.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholderContent:'',//placeholder 内容
    hotList:[], //热搜榜数据
    searchContent:'',//用户输入的表单项数据
    searchList:[], //关键字数据
    timer:null,  //定时器
    historyList:[]  //搜索历史记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInitData()
    //获取历史记录
    this.getSearchHistory()
  },

  //获取初始化数据
  async getInitData(){
     let placeholderData = await request('/search/default')
     let hotListData = await request('/search/hot/detail')
     this.setData({
      placeholderContent:placeholderData.data.showKeyword,
      hotList:hotListData.data
     })
  },

  //获取本地历史记录的功能函数
  getSearchHistory(){
    let historyList =  wx.getStorageSync('searchHistory')
    if(historyList){
      this.setData({
        historyList
      })
    }
  },

  //表单项内容发生变化改变的回调
   handleInputChange(e) {
     //更新数据
     this.setData({
       searchContent: e.detail.value.trim()
     })
     if (this.data.timer != null) {
       clearTimeout(this.data.timer)
     }
     this.data.timer = setTimeout(async () => {
       if (!this.data.searchContent) {
         this.setData({
           searchList: []
         })
         return
       }
       let {
         searchContent,
         historyList
       } = this.data;
       //发请求
       let searchListData = await request('/search', {
         keywords: searchContent,
         limit: 10
       })
       this.setData({
         searchList: searchListData.result.songs
       })
       //将搜索的关键字添加到搜索记录中
       if(historyList.indexOf(searchContent) !== -1){
        historyList.splice(historyList.indexOf(searchContent),1)
       }
       historyList.unshift(searchContent)
       this.setData({
         historyList
       })
       wx.setStorageSync('searchHistory', historyList)
     })
   },
    //清空搜索内容
    clearSearchContent(){
      this.setData({
        searchContent:'',
        searchList:[]
      })
    },
    //删除历史记录
    deleteSearchHistory(){
      wx.showModal({
        content:'确认删除吗？',
        success:(res)=>{
          if(res.confirm){
            this.setData({
              historyList:[]
            })
            //移出本地历史记录
            wx.removeStorageSync('searchHistory')
          }
        }
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})